package com.personnel.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.personnel.entity.User;
import com.personnel.service.IService;
import com.personnel.service.Service;

@WebServlet("/LogServlet")
public class LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public LogServlet() {
        super();
    }

    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String code=request.getParameter("code");
		String checkcode=(String) request.getSession().getAttribute("checkcode");
		if(code.equals(checkcode)==false){
			response.getWriter().write("验证码有误,三秒后跳转登录页面");
			response.addHeader("Refresh", "3;url=index.jsp");
			return;
		}

		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		IService se=new Service();
		User user=se.query(name, pwd);
		
		if(user!=null){		
			HttpSession s=request.getSession();
			s.setAttribute("admin", user);
//			if(user.getGrd().equals("管理员")){
				response.sendRedirect("main.jsp");
//			}		
		}else{
			response.getWriter().write("用户名或密码错误,三秒后跳转登录页面");
			response.addHeader("Refresh", "3;url=index.jsp");
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
